package com.yin.springboot.favorite.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yin.springboot.favorite.entity.Tag;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author Yin
 * @since 2025-08-16
 */
public interface TagMapper extends BaseMapper<Tag> {

    @Select("SELECT\n" +
            "	* \n" +
            "FROM\n" +
            "	tag a \n" +
            "WHERE\n" +
            "	EXISTS (\n" +
            "	SELECT\n" +
            "		* \n" +
            "	FROM\n" +
            "		tag_fav b\n" +
            "		JOIN fav c ON b.fid = c.id \n" +
            "	WHERE\n" +
            "	a.id = b.tid \n" +
            "	AND c.uid = #{uid})")
    List<Tag> selectByUid(Integer uid);

    @Select("select tag.*, #{uid} uid from tag where id=#{tid}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "{tid=id, uid=uid}", property = "favs",
                    // one = .. 一对一
                    // many = .. 多对多
                    many = @Many(select = "com.yc.favorite.mapper.FavMapper.queryByTidAndUid"))
            })
    Tag queryById(@Param("tid") int tid, @Param("uid") int uid);

}
